﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;

namespace YourMoney 
{
    /// <summary>
    /// Драйвер для работы с БД
    /// </summary>
    class SQLiteDriver
    {
        private string dbFileName;
        private SQLiteConnection connection;

        /// <summary>
        /// Соединение
        /// </summary>
        public SQLiteConnection Connection
        {
            get { return connection; }
        }
        /// <summary>
        /// Получить драйвер
        /// </summary>
        /// <param name="dnFileName"></param>
        /// <returns></returns>
        public static SQLiteDriver GetDrv(string dnFileName)
        {
            return new SQLiteDriver(dnFileName);
        }
        /// <summary>
        /// Создание драйвера
        /// </summary>
        /// <param name="dbFileName">Имя файла базы данных</param>
        private SQLiteDriver(string dbFileName)
        {
            try
            {
                this.dbFileName = dbFileName;
                if (!File.Exists(this.dbFileName))
                {
                    SQLiteConnection.CreateFile(this.dbFileName);
                }
                string connectionString = "Data Source=" + dbFileName;
                this.connection = new SQLiteConnection(connectionString);
                this.connection.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        /// <summary>
        /// Выполнение SQL-команды
        /// </summary>
        /// <param name="query">SQL-команда</param>
        /// <returns></returns>
        public int NonExecuteQuery(string query)
        {
            try
            {
                SQLiteCommand command = new SQLiteCommand(this.connection);
                command.CommandText = query;
                command.CommandType = CommandType.Text;
                return command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return 0;
            }
        }
        /// <summary>
        /// Получение данных по SQL-запросу
        /// </summary>
        /// <param name="query">Строка SQL-запроса</param>
        /// <returns></returns>
        public SQLiteDataReader Query(string query)
        {
            SQLiteCommand command = new SQLiteCommand(this.connection);
            command.CommandText = query;
            command.CommandType = CommandType.Text;
            SQLiteDataReader reader = command.ExecuteReader();
            return reader;
        }
        /// <summary>
        /// Разорвать соединение
        /// </summary>
        public void Close()
        {
            try
            {
                this.connection.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }

}